package cn.message.service.impl;

import cn.itcast.mapper.orderItemMapper;
import cn.itcast.mapper.orderMapper;
import cn.itcast.pojo.Performance;
import cn.itcast.pojo.order;
import cn.itcast.pojo.orderExample;
import cn.itcast.pojo.orderItem;
import cn.message.service.IPerformanceService;
import com.github.pagehelper.PageHelper;
import com.sun.media.jfxmedia.logging.Logger;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author 郭智超
 * @create 2020-04-10 16:39
 */
@Slf4j
@Service
public class PerformanceServiceImpl implements IPerformanceService {

    @Resource
    private orderMapper orderMapper;

    @Resource
    private orderItemMapper orderItemMapper;

    @Override
    public Map<String,Object> getAllPerformance(Integer page, Integer limit, Date startDate, Date endDate, String horsemanName , String adminPhone , String role) {
        //根据骑手openid分组查询骑手已完成交易的所有订单

        HashMap<String, Object> parameterMap = new HashMap<>();
        parameterMap.put("startDate",startDate);
        parameterMap.put("endDate",endDate);
        parameterMap.put("horsemanName",horsemanName);
        parameterMap.put("adminPhone",adminPhone);
        parameterMap.put("role",role);

        int count = orderMapper.selectPackageOrder(parameterMap).size();

        //分页
        PageHelper.startPage(page,limit);
        List<order> orders = orderMapper.selectPackageOrder(parameterMap);

        HashMap<String, Object> map = new HashMap<>();
        map.put("orders",orders);
        map.put("count",count);
        return map;
    }

    @Override
    public Map<String,Object> getAllPerformanceItem(Integer page,Integer limit,String horsemanOpenId, Date startDate, Date endDate) {

        orderExample orderExample = new orderExample();
        cn.itcast.pojo.orderExample.Criteria criteria = orderExample.createCriteria();
        criteria.andHorsemanAccountEqualTo(horsemanOpenId);
        criteria.andOrderStateEqualTo("已完成");
        if(startDate != null && endDate != null){
            criteria.andPayTimeBetween(startDate,endDate);
        }

        //获取骑手所有的orderid，并且将所有的orderid存入到list中
        List<cn.itcast.pojo.order> orders1 = orderMapper.selectByExample(orderExample);
        ArrayList<Integer> orderIds = new ArrayList<Integer>();
        for (cn.itcast.pojo.order order1 : orders1) {
            orderIds.add(order1.getId());
        }

        //根据骑手接单的所有orderid来统计骑手所有的订单中各商品的总重量和总金额
        int count = orderItemMapper.selectPackageItemOrder(orderIds).size();
        PageHelper.startPage(page,limit);
        List<orderItem> orderItems = orderItemMapper.selectPackageItemOrder(orderIds);

        HashMap<String, Object> map = new HashMap<>();
        map.put("orderItems",orderItems);
        map.put("count",count);
        return map;
    }
}
